<?php

namespace app\oa\controller;

use app\oa\model\Report;
use app\oa\model\User;
use think\Loader;
use think\Request;
use think\Session;

/**
 * @author: yfl
 * @QQ: 554665488
 * @description: 工作汇报区类
 * @time:
 * Class ReportController
 * @package app\oa\controller
 */
class ReportController extends BaseController
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */

    public function index()
    {

        $mobile = getSession('mobile');
        $data = User::getUserInfoByMobile($mobile);
//        $res = User::refactorObj($data);
//        halt($res);
        if (!empty($data)) {
            $reportData = Report::getReportInfoByUid($data, 0)->toArray();
//            $count = Report::getReportInfoByUidCount($data);
            $this->assign('lists', $reportData);
            $this->assign('data', $data);
//            $this->assign('count', $count);

        } else {
            $this->redirect('/login');
            self::showReturnCodeWithSaveLog(-1, '首页查询对象为空');
        }
        return $this->fetch('report/index');
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:  ajax下拉无限加载
     * @time:
     * @return \think\response\Json
     */
    public function ajaxGetList()
    {

        $mobile = getSession('mobile');
        $data = User::getUserInfoByMobile($mobile);
        if (Request::instance()->isAjax()) {
            $start = Request::instance()->param('start', 10, 'intval');
            $reportData = Report::getReportInfoByUid($data, $start)->toArray();

            if (!empty($reportData[0])) {
                return self::showReturnCodeWithOutData(1, '请求结果...', $reportData);
            } else {
                return self::showReturnCodeWithOutData(-1, '老铁.真的没有了...');
            }

        }

    }

    /**
     *
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $mobile = getSession('mobile');
        if (Request::instance()->isGet()) {
            $data = User::getUserInfoByMobile($mobile);
            $this->assign('data', $data);
            return $this->fetch('report/create');

        }

    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $mobile = getSession('mobile');
        if (Request::instance()->isPost()) {
            $comment = Request::instance()->param('content', '', '');
//            return self::showReturnCodeWithOutData(1,$comment);
            $data = User::getUserInfoByMobile($mobile);
            $res = User::refactorObj($data);

            $saveData = [
                'department_id' => $res->departmentObj['level'],
                'duty_id' => $data['duty_id'],
                'duty_id_level' => $res->duty_level,
                'context' => $comment,
            ];
            $res->reports()->save($saveData);
            if ($res) {
                return self::showReturnCodeWithOutData(1, '提交成功');
            } else {
                return self::showReturnCodeWithOutData(-1, '提交失败');
            }
        }
    }

    /**
     * @author: yfl
     * @QQ: 554665488
     * @description:未完成 待续
     * @time:
     * @param Request $request
     * @return mixed|\think\response\Json
     */
    public function search(Request $request)
    {
        if ($request->isAjax()) {

            $search = $request->post('search', null);
            if (empty($search)) {
                return false;
//                return self::showReturnCodeWithOutData(-1, '数据为空', []);
            }
            $res = Loader::model('User')->getUidBySearch($search);
            if(is_object($res)){
                $res = $res->getData();
                    $mobile = getSession('mobile');
                    $user_data = User::getUserInfoByMobile($mobile);
                    if ($res['duty_id'] > $user_data['duty_id']) {
                        return self::showReturnCodeWithOutData(-1, '数据为空', []);
                    } else {
                        $reportData = Loader::model('Report')->getSearchInfoByUid($res, 0);
                        return self::showReturnCodeWithOutData(1, '存在数据返回', $reportData);
                    }
            }else{
                return false;
//                return self::showReturnCodeWithOutData(-1, '数据为空', []);
            }
        }
    }


    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
//        $mobile = getSession('mobile');
//        $data = User::getUserInfoByMobile($mobile);
//        $page = Request::instance()->param('page', 2, 'intval');
//        $reportData = Report::getReportInfoByUid($data, $page)->toArray();
//        dump($reportData);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
